//先针对用户分析
const app =getApp();
Page({
  data: {  
    lists0:[], 
    lists1:[],
    lists2:[],
    editInfo:[],
    status:'',
    canvasName:'',
    canvasId:'',
    userImageurl:''
  }, 
  
  saveCanvasName: function(e){
   
   this.setData({canvasName:e.detail.value})
   let idStr = Date.now().toString(36)
   idStr += Math.random().toString(36).substr(3);
   var canvasId = idStr;
   //wx.setStorageSync("canvasId", canvasId);
   this.setData({canvasId:canvasId});
   //console.log(wx.getStorageSync("canvasId"));
  },
  addList: function(e){
    var that = this;
    console.log(e.target.dataset.index);
    
    if(e.target.dataset.index=='0'){  
    var  lists = that.data.lists0;  
    var newData = {};  
    lists.push(newData);//实质是添加lists数组内容，使for循环多一次  
    that.setData({  
      lists0: lists,  
    })  }
    else if (e.target.dataset.index == '1') {
      var lists = that.data.lists1;
      var newData = {};
      lists.push(newData);//实质是添加lists数组内容，使for循环多一次  
      that.setData({
        lists1: lists,
      })
    }
    else if (e.target.dataset.index == '2') {
      var lists = that.data.lists2;
      var newData = {};
      lists.push(newData);//实质是添加lists数组内容，使for循环多一次  
      that.setData({
        lists2: lists,
      })
    } else { console.log("index 传参错误")}
      
  },  
  delList: function (e) { 
    
   
    if(e.target.dataset.index=="0"){
    var lists = this.data.lists0;  
    lists.pop();      //实质是删除lists数组内容，使for循环少一次  
    this.setData({  
      lists0: lists,  
    }) }
    else if (e.target.dataset.index == "1") {
      var lists = this.data.lists1;
      lists.pop();      //实质是删除lists数组内容，使for循环少一次  
      this.setData({
        lists1: lists,
      })
    }
    else if (e.target.dataset.index == "2") {
      var lists = this.data.lists2;
      lists.pop();      //实质是删除lists数组内容，使for循环少一次  
      this.setData({
        lists2: lists,
      })
    }else{console.log("index 传参错误")}   
  },
  bindKeyInput: function(e){
    var that =this;
    var editinfo = that.data.editInfo;
    if (e.target.dataset.index == '0') { 
      var list = that.data.lists0;
      list[e.target.dataset.id] = e.detail.value;
      var newData= {"index1":0,"index2":e.target.dataset.id,"editor": app.globalData.userInfo.nickName}
      editinfo.push(newData);
      console.log(editinfo);
      that.setData({editInfo : editinfo})
      console.log(that.data.editInfo);
      that.setData({lists0:list})
      
    
     } else if (e.target.dataset.index == "1") {
       var list = that.data.lists1;
       list[e.target.dataset.id] = e.detail.value;
       that.setData({lists1: list});
       var newData = { "index1": 1, "index2": e.target.dataset.id, "editor": app.globalData.userInfo.nickName  }
       editinfo.push(newData);
       console.log(editinfo);
       that.setData({ editInfo: editinfo })
       console.log(that.data.editInfo);

    } else if (e.target.dataset.index == "2") {
      var list = that.data.lists2;
      list[e.target.dataset.id] = e.detail.value;
      that.setData({lists2 : list})
      var newData = { "index1": 2, "index2": e.target.dataset.id, "editor": app.globalData.userInfo.nickName }
      editinfo.push(newData);
      console.log(editinfo);
      that.setData({ editInfo: editinfo })
      console.log(that.data.editInfo);
    }else{console.log("error")}
    console.log(JSON.stringify(that.data.editInfo));
  },
  save: function(){
    var canvasname = this.data.canvasName;
    console.log("save"+canvasname);
    var creator = app.globalData.userInfo.nickName;
    console.log("save"+creator);
    var canvasId = this.data.canvasId;
    var userImageurl =app.globalData.userInfo.avatarUrl;
    console.log(userImageurl);
    wx.request({
      url: getApp().globalData.my_server+'/canvas/save',
      data:{
       canvasName: canvasname,
       canvasId: canvasId,
       creator: creator,
       lists0: this.data.lists0,
       lists1: this.data.lists1,
       lists2: this.data.lists2,
       editInfo: JSON.stringify(this.data.editInfo),
       userImageurl: userImageurl,
       stat: this.data.status
      },
      method:"POST",
      header:{
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function(res){
        console.log(res);
      },
      fail: function(){
        console.log("send error");
      }
    })
  },
  onLoad: function(options){
    var that = this;
    if(options.type!=null&&options.type==1){  //从canvas_typeofCanvas 传递过来，符合条件说明是新创建
      console.log(options.type)
      that.setData({status:"new"})
    }else{
      that.setData({status:"old"})
      console.log(options.creator+options.canvasName);
      wx.request({
        url: getApp().globalData.my_server+'/canvas/requestForcontent',
      method:'POST',
      header: { "Content-Type": "application/x-www-form-urlencoded"},
      data:{
        canvasName:options.canvasName,
        creator: options.creator
      },
      success:function(res){
       console.log(res);
       console.log("dedaode  "+res.data[0].canvasName);
       that.setData({canvasName:res.data[0].canvasName});
       var temList0=[];
       var temList1=[];
       var temList2=[];
       for(var o in res.data){
        for(var a in res.data[o].lists0){
          temList0.push(res.data[o].lists0[a]);
        }
        for (var a in res.data[o].lists1) {
          temList1.push(res.data[o].lists1[a]);
        }
        for (var a in res.data[o].lists2) {
          temList2.push(res.data[o].lists2[a]);
        }

       }
       that.setData({lists0:temList0,lists1:temList1,lists2:temList2});
       console.log("这些中间数组中的值："+JSON.stringify(that.data.lists0))
      }
    })
    }
  }    
  });